/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sfzlot.dev.netty.utils;

import cn.hutool.core.util.HexUtil;
import com.sfzlot.dev.netty.bean.Message;
import com.sfzlot.dev.netty.bean.bussiness.Industry;
import com.sfzlot.dev.netty.bean.bussiness.Product;
import com.sfzlot.dev.netty.bean.bussiness.Subdivide;
import com.sfzlot.dev.netty.bean.command.PowerSwitchCommand;
import com.sfzlot.dev.netty.bean.data.*;
import com.sfzlot.dev.netty.bean.data.powerswitch.*;
import com.sfzlot.dev.netty.bean.identifier.EncryptMethod;
import com.sfzlot.dev.netty.bean.identifier.ResponseIdentifier;
import com.sfzlot.dev.netty.bean.inf.Data;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * 消息处理工具类
 * 
 * @author Xieqc
 * @version 2019-06-20
 */
public class MessageUtils {

	/**
	 * Message对象转换为十六进制字符串
	 * 
	 * @param message
	 * @return
	 */
	public static String getHexString(Message message) {
		return HexUtil.encodeHexStr(getBytes(message));
//    	 return ByteBufUtil.hexDump(getBytes(message));
	}

	/**
	 * Message对象转换为字节数组
	 * 
	 * @param message
	 * @return
	 */
	public static byte[] getBytes(Message message) {
		if (message.getData() == null) {
			Data data = new ObjectData(new byte[0]);
			message.setData(data);
		}
		byte[] data = message.getData().toBytes();
		byte[] headerBytes = "##".getBytes();// 协议头
		byte[] by = new byte[3];
		by[0] = Integer.valueOf(message.getIndustry().getCode()).byteValue();// 行业大类
		by[1] = Integer.valueOf(message.getSubdivide().getCode()).byteValue();// 业务细分
		by[2] = Integer.valueOf(message.getProduct().getCode()).byteValue();// 产品标识
		// 版本号
		byte[] versionBytes = ByteUtil.shortToBytes(Integer.valueOf(message.getVersion()).shortValue());
		byte[] by2 = new byte[2];
		by2[0] = Integer.valueOf(message.getCommandIdentifier().getCode()).byteValue();// 命令标识
		by2[1] = Integer.valueOf(message.getResponseIdentifier().getCode()).byteValue();// 应答标识
		byte[] vinBytes = ByteUtil.stringToByte(message.getVin(), 17);// vin码
		byte[] serialBytes = ByteUtil.shortToBytes(Integer.valueOf(message.getSerial()).shortValue());// 序列号
		byte[] bz = new byte[1];
		bz[0] = Integer.valueOf(message.getEncryptMethod().getCode()).byteValue();// 加密方式
		byte[] lengthBytes = ByteUtil.shortToBytes(Integer.valueOf(data == null ? 0 : data.length).shortValue());// 包体长度
		byte[] all = ByteUtil.addAll(headerBytes, by, versionBytes, by2, vinBytes, serialBytes, bz, lengthBytes, data);// 包体
		if (data == null) {
			all = ByteUtil.addAll(headerBytes, by, versionBytes, by2, vinBytes, serialBytes, bz, lengthBytes);// 包体
		}

		// 校验码
		byte destCheckCharacter = ByteUtil.getByteCheckPassByLack(all);
		byte[] dest = new byte[1];
		dest[0] = destCheckCharacter;// 校验码
		byte[] alll = ByteUtil.addAll(all, dest);
		return alll;
	}

	/**
	 * 十六进制字符串转换为Message对象
	 * 
	 * @param hexString
	 * @return
	 * @throws Exception
	 */
	public static Message getMessage(String hexString) throws Exception {
//    	byte[] bytes = ByteUtil.hexStringToBytes(hexString);
//    	return getMessage(Unpooled.wrappedBuffer(bytes));
		byte[] messageBytes = HexUtil.decodeHex(hexString);
		return getMessage(messageBytes);
	}

	/**
	 * 字节转换为Message对象
	 * 
	 * @param bytes
	 * @return
	 * @throws Exception
	 */
	public static Message getMessage(byte[] bytes) throws Exception {
		return getMessage(Unpooled.wrappedBuffer(bytes));
	}

	/**
	 * 从字节码中读取Message信息
	 * 
	 * @param in
	 * @return
	 */
	public static Message getMessage(ByteBuf in) throws Exception {
		ByteBuf copyIn = in.copy();// 备份出来，校验码需要
		// 读取消息
		Message message = new Message();
		// 移动指针跳过起始符
		in.readByte();
		in.readByte();

		// 读取行业大类
		message.setIndustry(Industry.get(ByteUtil.getIntFromByte(in.readByte())));
		if (message.getIndustry() == null) {
			throw new Exception("读取行业大类不存在");
		}

		// 读取行业细分
		switch (message.getIndustry()) {
		case SMARTFAMILY:// 智慧家庭
			message.setSubdivide(Subdivide.SmartFamily.get(ByteUtil.getIntFromByte(in.readByte())));
			break;
		case RETAILSERVICE:// 零售服务
			message.setSubdivide(Subdivide.RetailService.get(ByteUtil.getIntFromByte(in.readByte())));
			break;
		default:
			throw new Exception("读取行业细分不存在");
		}

		// 读取产品类型
		if (message.getSubdivide() instanceof Subdivide.SmartFamily) {// 智慧家庭的细分行业
			message.setProduct(Product.SmartHome.get(ByteUtil.getIntFromByte(in.readByte())));

		} else if (message.getSubdivide() instanceof Subdivide.RetailService) {// 零售服务的细分行业
			switch ((Subdivide.RetailService) message.getSubdivide()) {
			case SELFSERVICE:// 自助终端
				message.setProduct(Product.SelfService.get(ByteUtil.getIntFromByte(in.readByte())));
				break;
			case INTELLIGENTADV:// 智能广告
				message.setProduct(Product.IntelligentAdv.get(ByteUtil.getIntFromByte(in.readByte())));
				break;
			default:
				throw new Exception("读取产品类型不存在");
			}
		} else {
			throw new Exception("读取产品类型不存在");
		}

		// 版本号
		message.setVersion(ByteUtil.getIntFromBytes(in.readByte(), in.readByte()));
		// 读取命令标识
		int commandCode = ByteUtil.getIntFromByte(in.readByte());
		// 读取应答标识
		message.setResponseIdentifier(ResponseIdentifier.getResponseIdentifier(ByteUtil.getIntFromByte(in.readByte())));
		// 获取唯一标识码
		byte[] vinBytes = new byte[17];
		for (int i = 0; i < vinBytes.length; i++) {
			vinBytes[i] = in.readByte();
		}
		message.setVin(new String(vinBytes).trim());
		// 读取命令序列号
		message.setSerial(ByteUtil.getIntFromBytes(in.readByte(), in.readByte()));
		// 读取数据单元加密方式
		message.setEncryptMethod(EncryptMethod.getEncryptMethod(ByteUtil.getIntFromByte(in.readByte())));
		// 移动指针跳过数据单元长度
		int bodyLength = ByteUtil.getIntFromBytes(in.readByte(), in.readByte());
		message.setBodyLength(bodyLength);
		// 读取数据单元
		message.setData(readBody(message, in, commandCode));
		in.readByte();// 读校验码

		// 判断校验是否通过
		message.setCheckPass(getCheckPass(copyIn));
		return message;
	}

	/**
	 * 获取校验码
	 *
	 * @param copyIn
	 * @return
	 */
	private static Boolean getCheckPass(ByteBuf copyIn) {
		int totalLength = copyIn.readableBytes();
		byte[] messageBytes = new byte[totalLength];
		copyIn.readBytes(messageBytes, 0, totalLength);
		return ByteUtil.getCheckPass(messageBytes);
	}

	/**
	 * 根据不同的业务类型、不同的命令类型来读取信息体
	 * @param message
	 * @param in
	 * @param commandCode
	 * @return
	 */
	private static Data readBody(Message message, ByteBuf in, int commandCode) throws Exception {
		message.setCommandIdentifier(PowerSwitchCommand.getCommand(commandCode));
		return dealPowerSwitchCommand(message, in);
	}

		/**
		 * 智慧家庭-智慧家居-智能开关的命令处理
		 *
		 * @param message
		 * @param in
		 * @return
		 */
		private static Data dealPowerSwitchCommand(Message message, ByteBuf in){
			switch ((PowerSwitchCommand) message.getCommandIdentifier()) {
				case DEVICE_LOGIN: // 设备登入
					if (message.getResponseIdentifier() == ResponseIdentifier.COMMAND) {// 请求登录报文
						DeviceLoginInData deviceLoginInData = new DeviceLoginInData();
						// 读取时间
						deviceLoginInData.setDataCollectTime(readDate(in));
						// 读取登入流水号
						deviceLoginInData.setSerialNum(ByteUtil.getIntFromBytes(in.readByte(), in.readByte()));
						// 读取ICCID码
						byte[] iccid = new byte[20];
						for (int i = 0; i < iccid.length; i++) {
							iccid[i] = in.readByte();
						}
						deviceLoginInData.setICCID(new String(iccid).trim());
						// 读取密码
						byte[] devicePwd = new byte[32];
						for (int i = 0; i < devicePwd.length; i++) {
							devicePwd[i] = in.readByte();
						}
						deviceLoginInData.setDevicePwd(new String(devicePwd).trim());
						return deviceLoginInData;
					} else {// 应答报文
						DeviceLoginOutData deviceLoginOutData = new DeviceLoginOutData();
						// 返回服务器登录时间
						deviceLoginOutData.setDataCollectTime(readDate(in));
						// 预留字段
						byte[] bakBytes = new byte[32];
						for (int i = 0; i < bakBytes.length; i++) {
							bakBytes[i] = in.readByte();
						}
						deviceLoginOutData.setBak(new String(bakBytes).trim());
						return deviceLoginOutData;
					}
				case DEVICE_STATUS: // 设备状态上报
					if (message.getResponseIdentifier() == ResponseIdentifier.COMMAND) {// 请求状态上报报文
						PowerSwitchStatusInData deviceStatusInData = new PowerSwitchStatusInData();
						// 读取时间
						deviceStatusInData.setDataCollectTime(readDate(in));
						// 设备状态
						deviceStatusInData.setStatus(ByteUtil.getIntFromByte(in.readByte()));
						if (message.getVersion() == 2) {//第二版本，添加了通讯类型
							deviceStatusInData.setTransType(ByteUtil.getIntFromByte(in.readByte()));
						}
						return deviceStatusInData;
					} else {// 应答报文
						DeviceStatusOutData deviceStatusOutData = new DeviceStatusOutData();
						// 返回服务器登录时间
						deviceStatusOutData.setDataCollectTime(readDate(in));
						return deviceStatusOutData;
					}
				case DEVICE_LOGOUT: // 设备登出
					if (message.getResponseIdentifier() == ResponseIdentifier.COMMAND) {// 请求登出报文
						DeviceLogoutInData deviceLogoutInData = new DeviceLogoutInData();
						// 读取时间
						deviceLogoutInData.setDataCollectTime(readDate(in));
						// 设备登录时登入流水号
						deviceLogoutInData.setSerialNum(ByteUtil.getIntFromBytes(in.readByte(), in.readByte()));
						return deviceLogoutInData;
					} else {// 应答报文
						return null;
					}
				case DEVICE_SERVER: // 设备更改服务器
					if (message.getResponseIdentifier() == ResponseIdentifier.COMMAND) {// 请求更改服务器报文
						DeviceServerInData deviceServerInData = new DeviceServerInData();
						// 读取URL
						byte[] serverUrlBytes = new byte[64];
						for (int i = 0; i < serverUrlBytes.length; i++) {
							serverUrlBytes[i] = in.readByte();
						}
						deviceServerInData.setServerUrl(new String(serverUrlBytes).trim());
						return deviceServerInData;
					} else {// 应答报文
						return null;
					}
				case DEVICE_SETTING: // 设备更改初使值
					if (message.getResponseIdentifier() == ResponseIdentifier.COMMAND) {// 请求更改设备初使值报文
						DeviceSettingInData deviceSettingInData = new DeviceSettingInData();
						// 读取ssid
						byte[] ssidBytes = new byte[32];
						for (int i = 0; i < ssidBytes.length; i++) {
							ssidBytes[i] = in.readByte();
						}
						deviceSettingInData.setSsid(new String(ssidBytes).trim());
						// 读取password
						byte[] passwordBytes = new byte[32];
						for (int i = 0; i < passwordBytes.length; i++) {
							passwordBytes[i] = in.readByte();
						}
						deviceSettingInData.setPassword(new String(passwordBytes).trim());
						return deviceSettingInData;
					} else {// 应答报文
						return null;
					}
				case DEVICE_INFO: // 获取设备信息
					if (message.getResponseIdentifier() == ResponseIdentifier.COMMAND) {// 请求获取设备信息报文
						DeviceInfoInData deviceInfoInData = new DeviceInfoInData();
						deviceInfoInData.setDataCollectTime(readDate(in));
						return deviceInfoInData;
					} else {// 应答报文
						PowerSwitchInfoOutData deviceInfoOutData = new PowerSwitchInfoOutData();
						// 读取时间
						deviceInfoOutData.setDataCollectTime(readDate(in));
						// 读取ssid
						byte[] ssidBytes = new byte[32];
						for (int i = 0; i < ssidBytes.length; i++) {
							ssidBytes[i] = in.readByte();
						}
						deviceInfoOutData.setSsid(new String(ssidBytes).trim());
						// 读取password
						byte[] passwordBytes = new byte[32];
						for (int i = 0; i < passwordBytes.length; i++) {
							passwordBytes[i] = in.readByte();
						}
						deviceInfoOutData.setPassword(new String(passwordBytes).trim());
						// 读取设备状态
						deviceInfoOutData.setStatus(ByteUtil.getIntFromByte(in.readByte()));
						// 获取多少秒后通/断电
						deviceInfoOutData.setTimes(
								ByteUtil.getIntFromFourBytes(in.readByte(), in.readByte(), in.readByte(), in.readByte()));
						// 物理开关是否有效
						deviceInfoOutData.setFlag(ByteUtil.getIntFromByte(in.readByte()));
						return deviceInfoOutData;
					}
				case DEVICE_START: // 通电
					if (message.getResponseIdentifier() == ResponseIdentifier.COMMAND) {// 请求通电报文
						PowerSwitchStartInData deviceStartInData = new PowerSwitchStartInData();
						deviceStartInData.setDataCollectTime(readDate(in));
						deviceStartInData.setTimes(
								ByteUtil.getIntFromFourBytes(in.readByte(), in.readByte(), in.readByte(), in.readByte()));
						return deviceStartInData;
					} else {// 应答报文
						PowerSwitchStartOutData deviceStartOutData = new PowerSwitchStartOutData();
						deviceStartOutData.setDataCollectTime(readDate(in));
						return deviceStartOutData;
					}
				case DEVICE_STOP: // 断电
					if (message.getResponseIdentifier() == ResponseIdentifier.COMMAND) {// 请求断电报文
						PowerSwitchStopInData deviceStopInData = new PowerSwitchStopInData();
						deviceStopInData.setDataCollectTime(readDate(in));
						deviceStopInData.setTimes(
								ByteUtil.getIntFromFourBytes(in.readByte(), in.readByte(), in.readByte(), in.readByte()));
						return deviceStopInData;
					} else {// 应答报文
						PowerSwitchStopOutData deviceStopOutData = new PowerSwitchStopOutData();
						deviceStopOutData.setDataCollectTime(readDate(in));
						return deviceStopOutData;
					}
				case DEVICE_RESET: // 重置
					return null;
				case DEVICE_SWITCH: // 物理开关是否有效
					if (message.getResponseIdentifier() == ResponseIdentifier.COMMAND) {// 请求物理开关是否有效报文
						PowerSwitchSwitchInData deviceSwitchInData = new PowerSwitchSwitchInData();
						deviceSwitchInData.setDataCollectTime(readDate(in));
						deviceSwitchInData.setFlag(ByteUtil.getIntFromByte(in.readByte()));
						return deviceSwitchInData;
					} else {// 应答报文
						PowerSwitchSwitchOutData deviceSwitchOutData = new PowerSwitchSwitchOutData();
						deviceSwitchOutData.setDataCollectTime(readDate(in));
						return deviceSwitchOutData;
					}
				default:
					break;
			}
			return null;
		}

		/**
		 * 根据字节数组读取时间
		 *
		 * @param in
		 * @return
		 */
		private static Date readDate(ByteBuf in){
			// 2000-2099
			int year = 2000 + ByteUtil.getIntFromByte(in.readByte());
			// 1-12
			int month = ByteUtil.getIntFromByte(in.readByte());
			// 1-31
			int day = ByteUtil.getIntFromByte(in.readByte());
			// 0-23
			int hour = ByteUtil.getIntFromByte(in.readByte());
			// 0-59
			int minute = ByteUtil.getIntFromByte(in.readByte());
			// 0-59
			int second = ByteUtil.getIntFromByte(in.readByte());
			Calendar calendar = Calendar.getInstance();
			// 注意Calendar的月份从0开始
			calendar.set(year, month - 1, day, hour, minute, second);
			return calendar.getTime();
		}

		public static void main(String[] args) throws Exception {
//			Message message = new Message();
//			testPowerSwitch(message);

			// 2323080130000102fe0000000000533030303031363545314644000101000715021916361201e9
			// 2323080130000202fe0000000000533030303031363545314644000101000715021916361201ea


			System.out.println(MessageUtils.getMessage("2323080130000102fe0000000000533030303031363545314644000101000715021916361201e9"));
		}


		/**
		 * 智能开关测试
		 *
		 * @param message
		 * @throws ParseException
		 * @throws Exception
		 */
		private static void testPowerSwitch(Message message) throws Exception{
			message.setIndustry(Industry.SMARTFAMILY);// 行业大类
			message.setSubdivide(Subdivide.SmartFamily.SMARTHOME);// 行业细分
			message.setProduct(Product.SmartHome.POWERSWITCH);// 产品类型
			message.setVersion(1);
			message.setVin("B0000012");
			message.setSerial(1);
			message.setEncryptMethod(EncryptMethod.NO_ENCRYPTION);

			// 智能开关-登录指令-客户端请求体
			Date date = new Date();
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_LOGIN);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			DeviceLoginInData deviceLoginInData = new DeviceLoginInData();
			deviceLoginInData.setDataCollectTime(date);
			deviceLoginInData.setICCID("1323123123");
			deviceLoginInData.setSerialNum((short) 1);
			message.setData(deviceLoginInData);
			String hexString = getHexString(message);
			System.out.println("智能开关-登录指令-客户端请求体HEX:" + hexString);
			System.out.println("智能开关-登录指令-客户端请求体Bean:" + getMessage(hexString));

			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_LOGIN);
			message.setResponseIdentifier(ResponseIdentifier.SUCCESS);
			DeviceLoginOutData deviceLoginOutData = new DeviceLoginOutData();
			deviceLoginOutData.setDataCollectTime(date);
			deviceLoginOutData.setBak("谢谢");
			message.setData(deviceLoginOutData);
			hexString = getHexString(message);
			System.out.println("智能开关-登录指令-服务端应答体HEX:" + hexString);
			System.out.println("智能开关-登录指令-服务端应答体Bean:" + getMessage(hexString));

			// 智能开关-状态上报指令-客户端请求体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_STATUS);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			PowerSwitchStatusInData deviceStatusInData = new PowerSwitchStatusInData();
			deviceStatusInData.setDataCollectTime(date);
			deviceStatusInData.setStatus(1);
			message.setData(deviceStatusInData);
			hexString = getHexString(message);
			System.out.println("智能开关-状态上报指令-客户端请求体HEX:" + hexString);
			System.out.println("智能开关-状态上报指令-客户端请求体Bean:" + getMessage(hexString));

			// 智能开关-状态上报指令-服务端应答体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_STATUS);
			message.setResponseIdentifier(ResponseIdentifier.SUCCESS);
			DeviceStatusOutData deviceStatusOutData = new DeviceStatusOutData();
			deviceStatusOutData.setDataCollectTime(date);
			message.setData(deviceStatusOutData);
			hexString = getHexString(message);
			System.out.println("智能开关-状态上报指令-服务端应答体HEX:" + hexString);
			System.out.println("智能开关-状态上报指令-服务端应答体Bean:" + getMessage(hexString));

			// 智能开关-设备登出指令-服务端请求体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_LOGOUT);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			DeviceLogoutInData deviceLogoutInData = new DeviceLogoutInData();
			deviceLogoutInData.setDataCollectTime(date);
			deviceLogoutInData.setSerialNum(1);
			message.setData(deviceLogoutInData);
			hexString = getHexString(message);
			System.out.println("智能开关-设备登出指令-服务端请求体HEX:" + hexString);
			System.out.println("智能开关-设备登出指令-服务端请求体Bean:" + getMessage(hexString));

			// 智能开关-设备登出指令-客户端应答体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_LOGOUT);
			message.setResponseIdentifier(ResponseIdentifier.SUCCESS);
			message.setData(null);
			hexString = getHexString(message);
			System.out.println("智能开关-设备登出指令-客户端应答体HEX:" + hexString);
			System.out.println("智能开关-设备登出指令-客户端应答体Bean:" + getMessage(hexString));

			// 智能开关-设备更改服务器指令-服务端请求体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_SERVER);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			DeviceServerInData deviceServerInData = new DeviceServerInData();
			deviceServerInData.setServerUrl("d.szjj.com:12580");
			message.setData(deviceServerInData);
			hexString = getHexString(message);
			System.out.println("智能开关-设备更改服务器指令-服务端请求体HEX:" + hexString);
			System.out.println("智能开关-设备更改服务器指令-服务端请求体Bean:" + getMessage(hexString));

			// 智能开关-设备更改服务器指令-设备端应答体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_SERVER);
			message.setResponseIdentifier(ResponseIdentifier.SUCCESS);
			message.setData(null);
			hexString = getHexString(message);
			System.out.println("智能开关-设备更改服务器指令-设备端应答体HEX:" + hexString);
			System.out.println("智能开关-设备更改服务器指令-设备端应答体Bean:" + getMessage(hexString));

			// 智能开关-设备更改初使值指令-服务端请求体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_SETTING);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			DeviceSettingInData deviceSettingInData = new DeviceSettingInData();
			deviceSettingInData.setSsid("MYZY");
			deviceSettingInData.setPassword("mayizhixing123456");
			message.setData(deviceSettingInData);
			hexString = getHexString(message);
			System.out.println("智能开关-设备更改初使值指令-服务端请求体HEX:" + hexString);
			System.out.println("智能开关-设备更改初使值指令-服务端请求体Bean:" + getMessage(hexString));

			// 智能开关-设备更改初使值指令-设备端应答体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_SETTING);
			message.setResponseIdentifier(ResponseIdentifier.SUCCESS);
			message.setData(null);
			hexString = getHexString(message);
			System.out.println("智能开关-设备更改初使值指令-设备端应答体HEX:" + hexString);
			System.out.println("智能开关-设备更改初使值指令-设备端应答体Bean:" + getMessage(hexString));

			// 智能开关-获取设备信息指令-服务端请求体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_INFO);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			DeviceInfoInData deviceInfoInData = new DeviceInfoInData();
			deviceInfoInData.setDataCollectTime(date);
			message.setData(deviceInfoInData);
			hexString = getHexString(message);
			System.out.println("智能开关-获取设备信息指令-服务端请求体HEX:" + hexString);
			System.out.println("智能开关-获取设备信息指令-服务端请求体Bean:" + getMessage(hexString));

			// 智能开关-获取设备信息指令-设备端应答体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_INFO);
			message.setResponseIdentifier(ResponseIdentifier.SUCCESS);
			PowerSwitchInfoOutData deviceInfoOutData = new PowerSwitchInfoOutData();
			deviceInfoOutData.setDataCollectTime(date);
			deviceInfoOutData.setSsid("huwei mate9");
			deviceInfoOutData.setPassword("@ERTYUI");
			deviceInfoOutData.setStatus(1);
			deviceInfoOutData.setTimes(100);
			deviceInfoOutData.setFlag(0);
			message.setData(deviceInfoOutData);
			hexString = getHexString(message);
			System.out.println("智能开关-获取设备信息指令-设备端应答体HEX:" + hexString);
			System.out.println("智能开关-获取设备信息指令-设备端应答体Bean:" + getMessage(hexString));

			// 智能开关-开关通电指令-服务端请求体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_START);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			PowerSwitchStartInData deviceStartInData = new PowerSwitchStartInData();
			deviceStartInData.setDataCollectTime(date);
			deviceStartInData.setTimes(100);
			message.setData(deviceStartInData);
			hexString = getHexString(message);
			System.out.println("智能开关-开关通电指令-服务端请求体HEX:" + hexString);
			System.out.println("智能开关-开关通电指令-服务端请求体Bean:" + getMessage(hexString));

			// 智能开关-开关通电指令-设备端应答体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_START);
			message.setResponseIdentifier(ResponseIdentifier.SUCCESS);
			PowerSwitchStartOutData deviceStartOutData = new PowerSwitchStartOutData();
			deviceStartOutData.setDataCollectTime(date);
			message.setData(deviceStartOutData);
			hexString = getHexString(message);
			System.out.println("智能开关-开关通电指令-设备端应答体HEX:" + hexString);
			System.out.println("智能开关-开关通电指令-设备端应答体Bean:" + getMessage(hexString));

			// 智能开关-开关断电指令-服务端请求体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_STOP);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			PowerSwitchStopInData deviceStopInData = new PowerSwitchStopInData();
			deviceStopInData.setDataCollectTime(date);
			deviceStopInData.setTimes(100);
			message.setData(deviceStopInData);
			hexString = getHexString(message);
			System.out.println("智能开关-开关断电指令-服务端请求体HEX:" + hexString);
			System.out.println("智能开关-开关断电指令-服务端请求体Bean:" + getMessage(hexString));

			// 智能开关-开关断电指令-设备端应答体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_STOP);
			message.setResponseIdentifier(ResponseIdentifier.SUCCESS);
			PowerSwitchStopOutData deviceStopOutData = new PowerSwitchStopOutData();
			deviceStopOutData.setDataCollectTime(date);
			message.setData(deviceStopOutData);
			hexString = getHexString(message);
			message.setData(null);
			System.out.println("智能开关-开关断电指令-设备端应答体HEX:" + hexString);
			System.out.println("智能开关-开关断电指令-设备端应答体Bean:" + getMessage(hexString));

			// 智能开关-重启指令-服务端请求体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_RESET);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			message.setData(null);
			hexString = getHexString(message);
			System.out.println("智能开关-重启指令-服务端请求体HEX:" + hexString);
			System.out.println("智能开关-重启指令-服务端请求体Bean:" + getMessage(hexString));

			// 智能开关-重启指令-设备端应答体 message.setBussiness(Bussiness.POWERSWITCH);
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_RESET);
			message.setResponseIdentifier(ResponseIdentifier.SUCCESS);
			message.setData(null);
			hexString = getHexString(message);
			System.out.println("智能开关-重启指令-设备端应答体HEX:" + hexString);
			System.out.println("智能开关-重启指令-设备端应答体Bean:" + getMessage(hexString));

			// 智能开关-物理开关是否有效-服务端请求体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_SWITCH);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			PowerSwitchSwitchInData deviceSwitchInData = new PowerSwitchSwitchInData();
			deviceSwitchInData.setDataCollectTime(date);
			deviceSwitchInData.setFlag(1);
			message.setData(deviceSwitchInData);
			hexString = getHexString(message);
			System.out.println("智能开关-物理开关是否有效指令-服务端请求体HEX:" + hexString);
			System.out.println("智能开关-物理开关是否有效指令-服务端请求体Bean:" + getMessage(hexString));

			// 智能开关-物理开关是否有效-设备端应答体
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_SWITCH);
			message.setResponseIdentifier(ResponseIdentifier.SUCCESS);
			PowerSwitchSwitchOutData deviceSwitchOutData = new PowerSwitchSwitchOutData();
			deviceSwitchOutData.setDataCollectTime(date);
			message.setData(deviceSwitchOutData);
			hexString = getHexString(message);
			message.setData(null);
			System.out.println("智能开关-物理开关是否有效指令-设备端应答体HEX:" + hexString);
			System.out.println("智能开关-物理开关是否有效指令-设备端应答体Bean:" + getMessage(hexString));
		}
}
